package cn.tedu.personal_patient.information.controller;


import cn.tedu.common.pojo.authentication.CurrentPrincipal;
import cn.tedu.common.validation.account.UserRules;
import cn.tedu.common.web.JsonResult;
import cn.tedu.personal_patient.information.pojo.param.StaffAvatarParam;
import cn.tedu.personal_patient.information.pojo.param.StaffParam;
import cn.tedu.personal_patient.information.pojo.param.StaffUpdateInfoParam;
import cn.tedu.personal_patient.information.pojo.vo.StaffVO;
import cn.tedu.personal_patient.information.services.IStaffService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Pattern;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/employees")
@Validated
@Api(tags = "2. 员工个人信息")
public class StaffController {
    @Autowired
    private IStaffService staffService;

    @GetMapping("")
    @ApiOperation("查询用户个人信息")
    @ApiOperationSupport(order = 410)
    public JsonResult listById(@AuthenticationPrincipal @ApiIgnore CurrentPrincipal currentPrincipal){
        List<StaffVO> list = staffService.listById(currentPrincipal);
        return JsonResult.ok(list);
    }

    @GetMapping("/staff/{id}")
    @ApiOperation("根据订单id查询员工个人信息")
    @ApiOperationSupport(order = 420)
    public JsonResult getById(@PathVariable Long id){
        StaffVO staff = staffService.getById(id);
        System.out.println(staff);
        return JsonResult.ok(staff);
    }

  /*  @PostMapping("/{id}/CardImgeUrl/update")
    @ApiOperation("修改工作证")
    @ApiOperationSupport(order = 400)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "imgeUrl", value = "新头像的路径", required = true, paramType = "query"),
            @ApiImplicitParam(name = "cardImgeUrl", value = "新工作证", required = true, paramType = "string")
    })
    public JsonResult updateCardImgeUrl(@PathVariable @Range(min = 1, message = "请提交合法的用户ID值！") Long id,
                                        @NotBlank String cardImgeUrl) {
        staffService.updateCardImgeUrl(id, cardImgeUrl);
        return JsonResult.ok();
    }

    @PostMapping("/{id}/picImgeRul/update")
    @ApiOperation("修改头像")
    @ApiOperationSupport(order = 420)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "long"),
            @ApiImplicitParam(name = "picImgeRul", value = "新头像的路径", required = true, paramType = "query")
    })
    public JsonResult updatePicImgeRul(@PathVariable @Range(min = 1, message = "请提交合法的用户ID值！") Long id,
                                   @NotBlank(message = "请提交新头像的路径") String picImgeRul) {
        staffService.updatePicImgeRul(id, picImgeRul);
        return JsonResult.ok();
    }

    @PostMapping("/{id}/phone/update")
    @ApiOperation("修改手机号码")
    @ApiOperationSupport(order = 430)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "手机号码", required = true, paramType = "query")
    })
    public JsonResult updatePhone(@PathVariable @Range(min = 1, message = "请提交合法的用户ID值！") Long id,
                                  @Pattern(regexp = UserRules.PATTERN_PHONE, message = UserRules.MESSAGE_PHONE_PATTERN) String phone) {
        staffService.updatePhone(id, phone);
        return JsonResult.ok();
    }

    @PostMapping("/{id}/homeAddress/update")
    @ApiOperation("修改家庭住址")
    @ApiOperationSupport(order = 440)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "homeAddress", value = "家庭住址", required = true, paramType = "query")
    })
    public JsonResult updateHomeAddress(@PathVariable @Range(min = 1, message = "请提交合法的用户ID值！") Long id,
                                        @Valid String homeAddress) {
        staffService.updateHomeAddress(id, homeAddress);
        return JsonResult.ok();
    }
    */
    @PostMapping("update")
    public JsonResult update(@AuthenticationPrincipal @ApiIgnore CurrentPrincipal currentPrincipal, StaffParam staffParam){
        log.error(currentPrincipal.getId()+"亿亿亿亿"+currentPrincipal.getUsername()+"亿亿亿亿"+staffParam);
        staffService.updateById(currentPrincipal,staffParam);
        return JsonResult.ok();
    }

    @PostMapping("avatar")
    public JsonResult avatar(@AuthenticationPrincipal @ApiIgnore CurrentPrincipal currentPrincipal, StaffAvatarParam staffAvatarParam){

        System.out.println("currentPrincipal = " + currentPrincipal + ", staffAvatarParam = !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" + staffAvatarParam);
        staffService.avatar(currentPrincipal,staffAvatarParam);
        return JsonResult.ok();
    }


}
